package com.jiukuaidao.zhulin.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import com.jiukuaidao.library.util.ToastUtil;

/**
 * Author : BinarySatan
 * Date : 2016/6/12
 * Description :
 */
public class IntentUtils {

    public static final int REQUEST_CODE_CALLPHONE = 1;

    public static void callPhone(Context context, String number) {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.CALL_PHONE)) {
                ToastUtil.show("您已禁止该权限，需要重新开启。", Toast.LENGTH_SHORT);
                return;
            }
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE_CALLPHONE);
        } else {
            realCallPhone(context, number);
        }
    }

    public static void realCallPhone(Context context, String number) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + number));//mobile为你要拨打的电话号码，模拟器中为模拟器编号也可
        context.startActivity(intent);
    }
}
